<template>
  <el-card>
    <el-row>
      <el-col :span="24">
        <div class="search">
        <el-button size="large" class="btn" type="primary" @click="add()" v-auth="auths.add">
          {{ $t('operation.add') }}
        </el-button>
          <div class="range">
            <el-date-picker v-model="range" 
            size="large" style="width: 250px;" class="range" type="datetimerange" 
            range-separator="To" :start-placeholder="$t('boat.startTime')" :end-placeholder="$t('boat.endTime')"
            format="DD/MM/YYYY HH:mm:ss" value-format="DD/MM/YYYY HH:mm:ss" @change="change" />
          </div>
          

          <el-select
            v-model="query.VesselsId"
            :placeholder="$t('boat.model')"
            size="large"
            class="inpt"
            filterable
            clearable
          >
            <el-option
              v-for="item in blist"
              :key="item.id"
              :label="item.model"
              :value="item.id"
            />
          </el-select>
          <el-input v-model="query.salePerson" size="large" :placeholder="$t('boat.saleMan')" clearable class="inpt"></el-input>
          <el-input v-model="query.salePlace" size="large" :placeholder="$t('boat.salePlace')" clearable class="inpt"></el-input>
          <el-input v-model="query.saleAmount" type="number" size="large" :placeholder="$t('boat.saleMoney')" class="inpt"></el-input>
          <el-button size="large" type="primary" @click="search()" v-auth="auths.add">
            {{ $t('operation.search') }}
          </el-button>
        </div>
      </el-col>
    </el-row>
    <el-table border style="margin: 10px 0px" :data="list">
      <el-table-column :label="$t('boat.model')" show-overflow-tooltip prop="model" />
      <el-table-column :label="$t('boat.type')" show-overflow-tooltip prop="typeName" />
      <el-table-column :label="$t('boat.cusName')" show-overflow-tooltip prop="phoneNumber">
        <template #="{ row, $index }">
          {{ row.customerFirstName }} {{ row.customerLastName }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('boat.cusPhone')" show-overflow-tooltip prop="phoneNumber" />
      <el-table-column :label="$t('boat.cusEmail')" show-overflow-tooltip prop="email" />
      <el-table-column :label="$t('boat.saleMan')" show-overflow-tooltip prop="salePerson" />
      <el-table-column :label="$t('boat.saleMoney')" show-overflow-tooltip prop="saleAmount" />
      <el-table-column :label="$t('boat.salePlace')" show-overflow-tooltip prop="salePlace" />
      <el-table-column :label="$t('boat.saleTime')" show-overflow-tooltip prop="saleDate" />
      <el-table-column :label="$t('boat.des')" show-overflow-tooltip prop="depict" />
      <el-table-column :label="$t('boat.vscode')" show-overflow-tooltip prop="vesselsCode" />
      <el-table-column label="操作" width="250px" fixed="right">
        <template #="{ row, $index }">
          <el-button
            type="primary"
            size="small"
            icon="Edit"
            v-auth="auths.edit"
            @click="add(row)"
          ></el-button>
          <el-popconfirm
            :title="`你确定要删除当前数据吗?`"
            width="200px"
            @confirm="removeRow(row.id)"
          >
            <template #reference>
              <el-button
                type="primary"
                size="small"
                v-auth="auths.del"
                icon="Delete"
              ></el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      v-model:current-page="pageNo"
      v-model:page-size="pageSize"
      :page-sizes="[10, 20, 30, 40]"
      :background="true"
      layout="prev, pager, next, jumper,->,sizes,total"
      :total="total"
      @current-change="getPage"
      @size-change="handler"
    />

    <Save ref="save" @callback="getPage()" />
  </el-card>
</template>

<script setup lang="ts">
import { ref, onMounted, reactive } from 'vue'
//引入请求
import { saleGetPage, saleDelete } from '@/api/boat/sale'
import { vesselsGetAll } from '@/api/boat/basic'
//引入ts类型
import { ElMessage } from 'element-plus'
import Save from './save.vue'
const previewUrl = ref(import.meta.env.VITE_IMG_SERVE)
const auths = {
  add: '/sys/user/add',
  edit: '/sys/user/update',
  del: '/sys/user/batchDelete',
  export: '/sys/user/exportXls',
}
const save = ref()
//分页器当前页码
let pageNo = ref<number>(1)
//每一页展示几条数据
let pageSize = ref<number>(10)
let total = ref<number>(0)
let list = ref([])
//控制抽屉显示与隐藏的字段
let drawer = ref<boolean>(false)
let skuInfo = ref<any>({})
//组件挂载完毕
onMounted(() => {
  getPage()
  getBoats()
})

const range = ref([])
const change = (e) => {
  console.log(e);
  if (e && e.length) {
    query.saleDateMin = e[0]
    query.saleDateMax = e[1]
  } else {
    query.saleDateMin = ''
    query.saleDateMax = ''
  }
}

let blist = ref([])
const getBoats = async () => {
  let { status, data, msg } = await vesselsGetAll()
  if (status == 200) {
    blist.value = data
  } else {
    ElMessage({ type: 'error', message: msg })
  }
}

const query = reactive({
  VesselsId: '',
  customerId: '',
  saleAmount: '',
  saleDateMax: '',
  saleDateMin: '',
  salePerson: '',
  salePlace: '',
})
const getPage = async (pager = 1) => {
  //当前分页器的页码
  pageNo.value = pager
  let { status, data, msg } = await saleGetPage({
    pageNum: pageNo.value,
    pageSize: pageSize.value,
    ...query
  })
  if (status == 200) {
    let { records, total: tatalNums } = data
    total.value = tatalNums
    list.value = records
  } else {
    ElMessage({ type: 'error', message: msg })
  }
}
const search = () => {
  getPage(1)
}
//分页器下拉菜单发生变化触发
const handler = (pageSizes: number) => {
  getPage()
}

const add = (row: any) => {
  save.value.show(row)
}

const removeRow = async (id: number) => {
  //删除某一个已有商品的情况
  let result: any = await saleDelete(id)
  if (result.status == 200) {
    //提示信息
    ElMessage({ type: 'success', message: '删除成功' })
    //获取已有全部商品
    getPage()
  } else {
    //删除失败
    ElMessage({ type: 'error', message: '系统数据不能删除' })
  }
}
</script>

<style scoped>
.search {
  width: 100%;
  display: flex;
  align-items: center;
}
.inpt {
  width: 120px;
  margin-right: 10px;
}
.range {
  width: 250px;
  margin-right: 10px;
}

.btn {
  margin-right: 10px;
}
</style>
